package com.hyyt.shopjsp.wxmg.material.service.imp;

import com.hyyt.shopjsp.basic.pojo.BasicKeyBook;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;
import com.hyyt.shopjsp.wxmg.material.dao.WxMaterialVideoInfoDAO;
import com.hyyt.shopjsp.wxmg.material.pojo.WxMaterialVideoInfo;
import com.hyyt.shopjsp.wxmg.material.service.IWxMaterialVideoInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * WX_素材视频信息Service 实现
 * LC
 */
@Service
public class WxMaterialVideoInfoService implements IWxMaterialVideoInfoService{

    private static  final Logger logger = Logger.getLogger(IWxMaterialVideoInfoService.class);

    @Resource
    private WxMaterialVideoInfoDAO wxMaterialVideoInfoDAO;

    /**
     * 保存WX_素材视频信息
     *
     * @param wxMaterialVideoInfo WX_素材视频信息
     * @return 保存后的WX_素材视频信息
     */
    @Override
    public void saveWxMaterialVideoInfo(WxMaterialVideoInfo wxMaterialVideoInfo) {
        String publicNoInfoStr = RedisDataReadUtil.getRedisValue(StringStaticKey.WX_PUBLIC_NO_INFO);
        Map<String,Object> publicNoInfo = FastJsonUtils.stringToCollect(publicNoInfoStr);
        if(Utils.objectIsNotEmpty(publicNoInfo) && Utils.objectIsNotEmpty(publicNoInfo.get("publicNumberId"))){
            wxMaterialVideoInfo.setPublicNumberId(Integer.parseInt(String.valueOf(publicNoInfo.get("publicNumberId"))));
        }
        String[] videoUrlStr = wxMaterialVideoInfo.getVideoUrl().split("/");
        wxMaterialVideoInfo.setVideoName(videoUrlStr[videoUrlStr.length-1]);
        String videoUrlFor = wxMaterialVideoInfo.getVideoUrl().substring(wxMaterialVideoInfo.getVideoUrl().lastIndexOf("."));
        if(videoUrlFor.equalsIgnoreCase(".mp4")){
            videoUrlFor = "mp4";
        }else if(videoUrlFor.equalsIgnoreCase(".flv")){
            videoUrlFor = "flv";
        }
        List<BasicKeyBook> keyBookList = RedisDataReadUtil.getBasicKeyBook("videoFormatEnumId");
        for(BasicKeyBook keyBook:keyBookList){
            if(videoUrlFor.equals(keyBook.getName())){
                wxMaterialVideoInfo.setVideoFormatEnumId(Integer.parseInt(keyBook.getValue()));
                break;
            }
        }
        wxMaterialVideoInfo.setDeletedFlag(0);
        wxMaterialVideoInfo.setInfoSendFlag(1);
        wxMaterialVideoInfoDAO.insert(wxMaterialVideoInfo);
    }

    /**
     * 更新WX_素材视频信息
     *
     * @param wxMaterialVideoInfo WX_素材视频信息
     * @return 更新后的WX_素材视频信息
     */
    @Override
    public void updateWxMaterialVideoInfo(WxMaterialVideoInfo wxMaterialVideoInfo) {
        wxMaterialVideoInfoDAO.update(wxMaterialVideoInfo);
    }

    /**
     * 还原指定ID的WX_素材视频信息
     *
     * @param id WX_素材视频信息ID
     */
    @Override
    public void restoreWxMaterialVideoInfo(Integer id) {
        Map <String,Object> paramMap=new HashMap<String,Object>();
        paramMap.put("materialVideoInfoId",id);
        paramMap.put("deletedFlag",0);
        wxMaterialVideoInfoDAO.update("restoreWxMaterialVideoInfo",paramMap);
    }

    /**
     * 删除指定ID的WX_素材视频信息
     *
     * @param id WX_素材视频信息ID
     */
    @Override
    public void deleteWxMaterialVideoInfo(Integer id) {
        wxMaterialVideoInfoDAO.delete(id);
    }

    /**
     * 删除指定ID集合的WX_素材视频信息
     *
     * @param ids WX_素材视频信息ID集合
     */
    public void deleteWxMaterialVideoInfo(Collection<Integer> ids){
        wxMaterialVideoInfoDAO.batchDelete(ids);
    }

    /**
     * 根据ID加载一个WX_素材视频信息
     *
     * @param id WX_素材视频信息ID
     * @return WX_素材视频信息
     */
    @Override
    public WxMaterialVideoInfo loadWxMaterialVideoInfo(Integer id) {
        return wxMaterialVideoInfoDAO.load(id);
    }

    /**
     * 查询所有的WX_素材视频信息，通常有最多取30000条记录的限制
     *
     * @return 所有的WX_素材视频信息，通常有最多取30000条记录的限制
     */
    @Override
    public List<WxMaterialVideoInfo> queryAllWxMaterialVideoInfo() {
        return wxMaterialVideoInfoDAO.queryAll();
    }

    /**
     * 查询WX_素材视频信息
     *
     * @param params 查询参数，为key-value形式
     * @return WX_素材视频信息集合
     */
    @Override
    public List<WxMaterialVideoInfo> queryWxMaterialVideoInfo(Map<String, Object> params) {
        return wxMaterialVideoInfoDAO.query(params);
    }

    /**
     * 查询WX_素材视频信息
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return WX_素材视频信息集合
     */
    @Override
    public List<WxMaterialVideoInfo> queryWxMaterialVideoInfo(Map<String, Object> params, int startRow, int rowSize) {
        return wxMaterialVideoInfoDAO.query(params, startRow, rowSize);
    }

    /**
     * 统计WX_素材视频信息
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    @Override
    public int countWxMaterialVideoInfo(Map<String, Object> params) {
        return wxMaterialVideoInfoDAO.count(params);
    }
}
